import { Context, Next } from "koa";
import { config } from "../config";
import { getaccessLogger } from "../logger";
import { verify } from "../utils/auth";
import { resp } from "../utils/res";

const mw = async (ctx: Context, next: Next) => {
  //每一个进入此系统的操作
  return next();
};
const fverify = async (ctx: Context, next: Next) => {
  //免token白名单 在env中写入
  let whitePath = config.white_path;
  if (whitePath.includes(ctx.path)) {
    return next();
  } else {
    let token = ctx.headers.authorization?.split(" ")[1] as string;
    if (!token) {
      // ctx.state.status = 403;
      return resp.error(ctx, "无token禁止访问");;
    }
    let res = verify(token);
    if (res.code == 200) {
      return next();
    } else {
      return resp.error(ctx, "token验证错误");
    }
  }
};
export { mw, fverify };
